package com.supreme.stockserver.service;

import com.supreme.common.entity.ProductQuality;
import com.supreme.commonmybatis.mapper.ProductMapper;
import com.supreme.commonentity.mysql.primary.BizProduct;
import com.supreme.commonentity.mysql.primary.BizQuality;
import com.supreme.commonboot.exception.BizException;
import com.supreme.stockserver.service.remote.FeignApiService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private FeignApiService feignApiService;

    @Transactional(rollbackFor = BizException.class)
    public int createProduct(BizProduct product) {
        if (product.getProductCode().equals("404")) {
            throw new BizException("productCode is 404");
        }
        final int insert = productMapper.insert(product);
        return insert;
    }

    @GlobalTransactional
    @Transactional(rollbackFor = BizException.class)
    public void createProductQuality(ProductQuality productQuality) {
        final Integer domainType = productQuality.getDomainType();
        final String createCode = productQuality.getCreateCode();
        final BizProduct product = new BizProduct().setDomainType(domainType).setProductCode(createCode);
        productMapper.insert(product);

        BizQuality quality = new BizQuality().setDomainType(domainType).setQualityCode(createCode);
        feignApiService.createQuality(quality);
    }
}
